package com.wsd.common;

import com.wsd.dao.LoanInformationMapper;
import com.wsd.domain.Loan;
import com.wsd.domain.LoanInformation;
import com.wsd.domain.RepayPlan;
import com.wsd.domain.RepaymentPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Wan Yucan
 * @date 2020/7/20-10:14
 */
@Component
public class TypeSwitch {
    @Autowired
    private LoanInformation loanInformation;
    @Autowired
    private RepaymentPlan repaymentPlan;
    @Autowired TypeSwitch typeSwitch;
    /**
     * 根据Loan数据计算LoanInformation信息，用于数据库存储
     * @param loan
     * 贷款信息类
     * @return
     * 贷款相信信息，用于数据库操作
     */
    public LoanInformation loanToLoanInformation(Loan loan){
        loanInformation.setCustomerId(loan.getUserAccount());
        loanInformation.setRepaymentType(String.valueOf(loan.getRepaymentType()));
        loanInformation.setInstallmentNum(String.valueOf(loan.getInstallmentNum()));
        loanInformation.setLoanAmount(loan.getLoanAmount());
        loanInformation.setLoanStatus("0");
        loanInformation.setRemainPrincipal(loan.getLoanAmount());
        loanInformation.setRepaymentDate(String.valueOf(loan.getLoanDate().getDayOfMonth()));
        loanInformation.setExpireDate(loan.getLoanDate().toLocalDate().plusMonths(loan.getInstallmentNum()));
        loanInformation.setLoanStartTime(loan.getLoanDate());
        return loanInformation;
    }

    /**
     * 还款计划类转为还款计划表中的类，用于数据库存储
     * @param repayPlan
     * @return
     */
    public RepaymentPlan repayPlanToRepaymentPlan(RepayPlan repayPlan) {
        repaymentPlan.setPrincipalPart(repayPlan.getPrincipalPart());
        repaymentPlan.setRepayableAmount(repayPlan.getRepayableAmount());
        repaymentPlan.setInterestPart(repayPlan.getInterestPart());
        repaymentPlan.setInstallmentIdx(repayPlan.getInstallmentIdx());
        repaymentPlan.setRepaymentStatus("0");
        repaymentPlan.setRepaymentTime(repayPlan.getRepaymentDate());
        return repaymentPlan;
    }
}
